package com.cameronyule.echonest.abstract
{
	import com.cameronyule.echonest.api.EchonestRequest;
	import com.cameronyule.echonest.events.EchonestResponseEvent;

	/*abstract*/ public class APIClass
	{
		protected var apiKey:String;
		protected var apiVersion:String;
		
		public function APIClass(apiKey:String, apiVersion:String)
		{
			this.apiKey = apiKey;
			this.apiVersion = apiVersion;
		}
		
		protected function call(method:String, parameters:Object, requestMethod:String = ''):void
		{
			parameters['version'] = apiVersion;
			parameters['api_key'] = apiKey;
			
			var request:EchonestRequest = new EchonestRequest(method, parameters, requestMethod);
			request.addEventListener(EchonestResponseEvent.RESPONSE, responseHandler);			
		}
		
		private function responseHandler(event:EchonestResponseEvent):void
		{
			var request:EchonestRequest = EchonestRequest(event.target);
			request.removeEventListener(EchonestResponseEvent.RESPONSE, responseHandler);
			
			trace(event.response);
		}
	}
}